package com.qk.controller;

import com.github.pagehelper.Page;
import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/roles")
@RestController
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result query(String name, String label, @RequestParam (defaultValue = "1") Integer page, @RequestParam (defaultValue = "10") Integer pageSize) {
        log.info("查询所有角色");
        PageResult<Role> pageResult = roleService.query(name, label, page, pageSize);
        return Result.success(pageResult);
    }
    @DeleteMapping ("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        log.info("删除角色", id);
        roleService.deleteRole(id);
        return Result.success();
    }
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色", role);
        roleService.addRole(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("查询角色", id);
        Role role = roleService.selectById(id);
        return Result.success(role);
    }
    @Log
    @PutMapping
    public Result updata(@RequestBody Role role) {
        log.info("修改角色", role);
        roleService.updateRole(role);
        return Result.success();
    }
    @GetMapping("/list")
    public Result findAll() {
        log.info("查询所有角色");
        List<Role> roles = roleService.findAll();
        return Result.success(roles);
    }


}
